package script.mainpage.items
{
	import eventUtil.EventCenter;
	
	import laya.events.Event;
	
	import model.ConstantModel;
	import model.HttpErrorCode;
	import model.HttpRequestUtil;
	import model.Userdata;
	import model.goods.UserCartGoodsVo;
	
	import script.ViewManager;
	
	import ui.items.GoodsCartItemUI;
	
	public class UserCartGoodsCell extends GoodsCartItemUI
	{
		private var userCartVo:UserCartGoodsVo
		public function UserCartGoodsCell()
		{
			super();
		}
		
		public function setData(data:UserCartGoodsVo):void
		{
			userCartVo = data;
			this.goodsPic.skin = userCartVo.goods.goodsImg;
			
			this.selectgoods.selected = userCartVo.selected;
			
			this.selectgoods.on(Event.CLICK,this,selectGoods);
			
			//this.sellPriceLbl.text = "¥" + goodsVo.sellPrice.toFixed(2);
			this.countlbl.text = userCartVo.goodsCount + "";
			var totalprice:String = (userCartVo.goods.sellPrice * userCartVo.goodsCount).toFixed(2);
			this.sellPriceLbl.text = "¥" + totalprice;
			
			this.cheapbox.visible = false;
			
			this.goodsNameLbl.text = "1份" + userCartVo.goods.unitAmount + (userCartVo.goods.offWeight != "0"?"±" + userCartVo.goods.offWeight:"") + ConstantModel.UNIT_NAME[userCartVo.goods.unit] + "|" + userCartVo.goods.goodsName;
			
			this.minusBtn.on(Event.CLICK,this,onSubNum);
			this.plusBtn.on(Event.CLICK,this,onPlusNum);

		}
		
		private function onSubNum():void
		{
			if(userCartVo.goodsCount > 1)
			{
				updateCount(userCartVo.goodsCount-1);
			}
			else
			{
				ViewManager.instance.openView(ViewManager.VIEW_POPUPDIALOG,false,{msg:"确定删除该商品吗？",cancel:"我再想想",caller:this,callback:confirmDelete});
				return;
			}
		}
		private function onPlusNum():void
		{
			
			updateCount(userCartVo.goodsCount+1);
			
			
		}
		
		private function confirmDelete(b:Boolean):void
		{
			if(b)
			{
				updateCount(userCartVo.goodsCount-1);

			}
		}
		private function updateCount(count:int):void
		{
			var postdata:String = "cartId=" + userCartVo.id + "&count=" + count;
			HttpRequestUtil.instance.Request(HttpRequestUtil.httpUrl + HttpRequestUtil.updateGoodCount,this,updateCountsBack,postdata,"post");
		}
		
		private function updateCountsBack(data:Object):void
		{
			var result:Object = JSON.parse(data as String);
			if(result.code == HttpErrorCode.OK)
			{
				EventCenter.instance.event(EventCenter.UPDATE_USERCART_GOODS_COUNT,[result.data.cartId,result.data.num]);

			}
		}
		private function selectGoods():void
		{
			EventCenter.instance.event(EventCenter.SELECTE_USERCART_GOODS,userCartVo.id);
			
		}
		
	}
}